<template>
  <div class="main-content">
    <!-- 列表页 -->
    <div v-if="showFlag">
      <el-form
        :inline="true"
        :model="searchForm"
        class="search-form"
      >
        <el-row
          :gutter="20"
          class="slt"
        >
          <el-form-item label="标题">
            <el-input
              prefix-icon="el-icon-search"
              v-model="searchForm.title"
              placeholder="标题"
              clearable
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button
              icon="el-icon-search"
              type="success"
              @click="search()"
            >查询</el-button>
          </el-form-item>
        </el-row>

        <el-row class="ad">
          <el-form-item>
            <el-button
              v-if="isAuth('news', '新增')"
              type="success"
              icon="el-icon-plus"
              @click="addOrUpdateHandler()"
            >新增</el-button>
            <el-button
              v-if="isAuth('news', '删除')"
              :disabled="dataListSelections.length <= 0"
              type="danger"
              icon="el-icon-delete"
              @click="deleteHandler()"
            >删除</el-button>
          </el-form-item>
        </el-row>
      </el-form>
      <div class="table-content">
        <el-table class="tables"
            :size="'medium'"
            :show-header="true"
            :border="true"
            :fit="true"
            :stripe="true"
            :style="{width: '100%',fontSize:'14px',color:'#606266'}"
            v-if="true"
            :data="dataList"
            v-loading="dataListLoading"
            @selection-change="selectionChangeHandler">
            <el-table-column
                type="selection"
                align="center"
                width="50">
            </el-table-column>
            <el-table-column label="索引" align="center" type="index" width="50" />
            <el-table-column
                align="center"
                prop="title"
                header-align="center"
                label="标题">
                <template slot-scope="scope">
                  {{scope.row.title}}
                </template>
            </el-table-column>
            <el-table-column
                align="center"
                prop="picture"
                header-align="center"
                width="200"
                label="图片">
                <template slot-scope="scope">
                <div v-if="scope.row.picture" class="table-image">
                    <img :src="$base.url+scope.row.picture.split(',')[0]" class="table-img">
                  </div>
                  <div v-else>无图片</div>
                </template>
              </el-table-column>
            <el-table-column
                width="300"
                align="center"
                header-align="center"
                label="操作"
                fixed="right"
                class-name="operation-column">
                <template slot-scope="scope">
                    <div class="operation-buttons">
                        <el-button
                            type="success"
                            icon="el-icon-tickets"
                            size="mini"
                            @click="addOrUpdateHandler(scope.row.id,'info')">
                            详情
                        </el-button>
                        <el-button
                            v-if="isAuth('news','修改')"
                            type="primary"
                            icon="el-icon-edit"
                            size="mini"
                            @click="addOrUpdateHandler(scope.row.id)">
                            修改
                        </el-button>
                        <el-button
                            type="danger"
                            icon="el-icon-delete"
                            size="mini"
                            @click="deleteHandler(scope.row.id)">
                            删除
                        </el-button>
                    </div>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination
          clsss="pages"
          :layout="layouts"
          @size-change="sizeChangeHandle"
          @current-change="currentChangeHandle"
          :current-page="pageIndex"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="10"
          :total="totalPage"
          class="pagination-content"
        ></el-pagination>
      </div>
    </div>
    <!-- 添加/修改页面  将父组件的search方法传递给子组件-->
    <add-or-update v-if="addOrUpdateFlag" :parent="this" ref="addOrUpdate"></add-or-update>
  </div>
</template>
<script>
import axios from 'axios'
import AddOrUpdate from "./add-or-update";
export default {
  data() {
    return {
      searchForm: {
        key: ""
      },
      form:{},
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      showFlag: true,
      sfshVisiable: false,
      shForm: {},
      chartVisiable: false,
      addOrUpdateFlag:false,
      layouts: 'total, sizes, prev, pager, next, jumper',
    };
  },
  created() {
    this.init();
    this.getDataList();
  },
  mounted() {

  },
  filters: {
    htmlfilter: function (val) {
      return val.replace(/<[^>]*>/g).replace(/undefined/g,'');
    }
  },
  components: {
    AddOrUpdate,
  },
  methods: {
    init () {
    },
    search() {
      this.pageIndex = 1;
      this.getDataList();
    },

    // 获取数据列表
    getDataList() {
      this.dataListLoading = true;
      let params = {
        page: this.pageIndex,
        limit: this.pageSize,
        sort: 'id',
      }
      if(this.searchForm.title!=='' && this.searchForm.title!==undefined){
        params['title'] = '%' + this.searchForm.title + '%'
      }
      this.$http({
        url: "news/page",
        method: "get",
        params: params
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.dataList = data.data.list;
          this.totalPage = data.data.total;
        } else {
          this.dataList = [];
          this.totalPage = 0;
        }
        this.dataListLoading = false;
      });
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getDataList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.getDataList();
    },
    // 多选
    selectionChangeHandler(val) {
      this.dataListSelections = val;
    },
    // 添加/修改
    addOrUpdateHandler(id,type) {
      this.showFlag = false;
      this.addOrUpdateFlag = true;
      this.crossAddOrUpdateFlag = false;
      if(type!=='info'){
        type = 'else';
      }
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id,type);
      });
    },
    // 查看评论
    // 下载
    download(file){
      window.open(`${file}`)
    },
    // 删除
    deleteHandler(id) {
      var ids = id
        ? [Number(id)]
        : this.dataListSelections.map(item => {
            return Number(item.id);
          });
      this.$confirm(`确定进行[${id ? "删除" : "批量删除"}]操作?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.$http({
          url: "news/delete",
          method: "post",
          data: ids
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.$message({
              message: "操作成功",
              type: "success",
              duration: 1500,
              onClose: () => {
                this.search();
              }
            });
          } else {
            this.$message.error(data.msg);
          }
        });
      });
    },
  }
};
</script>
<style lang="scss" scoped>
  .slt {
    margin: 0 !important;
    display: flex;
  }

  .ad {
    margin: 0 !important;
    display: flex;
  }

.pagination-content {
  display: flex;
  justify-content: center;
  margin-top: 20px;
}

.table-content {
  margin-top: 20px;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.05);
  background-color: #ffffff;
  }

  .tables {
  width: 100%;
}

.tables .el-table__header th {
  background-color: #f5f7fa !important;
  color: #606266;
  font-weight: bold;
  border-color: #ebeef5;
}

.tables .el-table__row {
  background-color: #ffffff !important;
}

.tables .el-table__row:hover {
  background-color: #f5f7fa !important;
}

.tables .el-table__row td {
  padding: 10px 0;
  color: #606266 !important;
}

.el-pagination {
  margin-top: 20px;
  text-align: center;
}

.search-form {
  margin-bottom: 20px;
  padding: 20px;
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.05);
}

.search-form .el-form-item {
  margin-bottom: 10px;
}

.search-form .el-input__inner {
  border-radius: 4px;
  border-color: #dcdfe6;
}

.search-form .el-input__inner:hover {
  border-color: #c0c4cc;
}

.search-form .el-input__inner:focus {
  border-color: #4a90e2;
}

.search-form .el-button {
  border-radius: 4px;
  padding: 10px 20px;
  transition: all 0.3s;
}

.search-form .el-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.table-image {
  display: flex;
  justify-content: center;
  align-items: center;
}

.table-img {
  width: 100px;
  height: 100px;
  object-fit: cover;
  border-radius: 4px;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.1);
}

.table-img:hover {
  transform: scale(1.05);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

/* 操作按钮样式优化 */
.operation-buttons {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 5px;
  padding: 5px 0;
}

.operation-buttons .el-button {
  margin: 3px;
  flex-grow: 0;
  min-width: 74px;
  font-size: 12px;
  height: 32px;
  padding: 0 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 4px;
  transition: all 0.25s ease;
}

.operation-buttons .el-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

.operation-buttons .el-button--success {
  background-color: #67c23a;
  border-color: #67c23a;
}

.operation-buttons .el-button--primary {
  background-color: #409eff;
  border-color: #409eff;
}

.operation-buttons .el-button--danger {
  background-color: #f56c6c;
  border-color: #f56c6c;
}

/* 当表格列宽度不够时，确保操作按钮区域仍然美观 */
.operation-column {
  .cell {
    padding: 5px !important;
    overflow: visible !important;
  }
}

/* 覆盖默认的表格背景色 */
/deep/ .el-table,
/deep/ .el-table tr,
/deep/ .el-table__body {
  background-color: #ffffff !important;
}

/deep/ .el-table th {
  background-color: #f5f7fa !important;
}

/deep/ .el-table--striped .el-table__body tr.el-table__row--striped td {
  background-color: #fafafa;
}

/* 修改表头和整体表格样式 */
/deep/ .el-table th.is-leaf {
  border-bottom: 1px solid #ebeef5;
}

/deep/ .el-table__fixed-right::before,
/deep/ .el-table__fixed::before {
  background-color: #ebeef5;
}

.main-content {
  background-color: #f5f7fa;
  padding: 20px;
  min-height: 100%;
}
</style>
